class ModuleBase extends GYSprite {
	
	public uiPath: string;
	public uiID: string;
	public url: string;
	
	protected _baseData: any;
    protected _screenMode: string;    
	protected _moduleScale: number;
	protected _designWidth: number;
	protected _designHeight: number;
	protected _designScale:number;
	protected _designMaxRatio:number;
	protected _designMinRatio:number;
	protected _orientation:string;
	protected _offsetLeft:number;
    protected _offsetRight:number;
    protected _offsetTop:number;
    protected _offsetBottom:number;
	protected _frameBound:Laya.Rectangle;
	
	protected _aborted:boolean;

	/**模块运行时产生的数据，可以附加到这个对象，通过Dispatch派发事件时(如Dispatch.MODULE_ABORT)，一并派发出去从而被外部接收到*/
	protected _module_data:any;
	protected _globalDispatcher:Dispatcher;
	public constructor() {
		super();
		let s = this;
		
		s._globalDispatcher = Dispatcher.getInstance();
		s.width = GameManager.DESIGN_WIDTH;
		s.height = GameManager.DESIGN_HEIGHT;	
		s._frameBound = new Laya.Rectangle();			
		s.on(Laya.Event.ADDED,s,s.addToStage);
		this.modulePreStart();
	}
	/**重新适配模块
	 * @param w 模块的设计尺寸 宽度
	 * @param h 模块的设计尺寸 高度
	 * @param screenMode 适配方案参考LayerManager常量
	 * @param maxRatio 最宽的比例限制 默认NaN 无限制
	 * @param minRatio 最窄尺寸限制 默认1.333 4比3限制
	 * @param scale 设计的缩放比例，方便适配不同大小设备，前提是内容需要做成相对布局，否则会定位超出屏幕范围
	 * @param orientation 旋转方向	 
	*/
	public resetSize(w: number, h: number, screenMode: string, maxRatio: number = 0.75, minRatio: number = NaN,scale:number=1,orientation:string=null) {
		let s = this;		
		let shouldRotate:boolean;		
		s._orientation = orientation;
		// shouldRotate = s._orientation !=null && Laya.stage.orientation != s._orientation;
		s._designMaxRatio = maxRatio;
		s._designMinRatio = minRatio;
		s._designScale = scale;
		s._designWidth = w;
		s._designHeight = h;
		
		w *= scale;
		h *= scale;
		s.width = w;
		s.height = h;

		let sclX: number, sclY: number, scl: number;
		let game: GameManager;		
		let stdW: number, stdH: number;
		let boundW:number,boundH:number;
		game = GameManager.getInstance();		
		boundW = GameManager.STAGE_WIDTH;
		boundH = GameManager.STAGE_HEIGHT;
		s._screenMode = screenMode;
		scl = boundW / boundH;
		stdW = boundW;
		stdH = boundH;
		if (scl > maxRatio)
			stdW = boundH * maxRatio;
		else if (scl < minRatio)
			stdH = boundW * minRatio;

		if(shouldRotate)
		{
			sclX = stdH / w;
			sclY = stdW / h;
		}
		else
		{
			sclX = stdW / w;
			sclY = stdH / h;
		}		
		

		if (s._screenMode == ScreenMode.FIX_WIDTH)
			s._moduleScale = sclX;
		else if (s._screenMode == ScreenMode.FIX_HEIGHT)
			s._moduleScale = sclY;
		else if (s._screenMode == ScreenMode.NOBORD)
			s._moduleScale = sclX > sclY ? sclX : sclY;
		else if (s._screenMode == ScreenMode.SHOWALL)
			s._moduleScale = sclX < sclY ? sclX : sclY;
		else
			s._moduleScale = 1;		
		s.scaleX = s.scaleY = s._moduleScale;
		if(shouldRotate)
		{
			s.rotation = -90;			
			s.x = (boundW - h * s._moduleScale >> 1);
			s.y = (boundH - w * s._moduleScale >> 1) + (w * s._moduleScale);
			s._offsetLeft = s._offsetRight = s.y - (w * s._moduleScale);
			s._offsetTop = s._offsetBottom = s.x;
		}
		else
		{			
			s.x = (boundW - w * s._moduleScale >> 1);
			s.y = (boundH - h * s._moduleScale >> 1);
			s._offsetLeft = s._offsetRight = s.x;
			s._offsetTop = s._offsetBottom = s.y;
		}		
	}
	
	/**模块启动之前*/
	public modulePreStart(): void {
		let s = this;
		s.resetSize(GameManager.DESIGN_WIDTH,GameManager.DESIGN_HEIGHT,GameManager.SCALE_MODE,GameManager.MAX_RATIO,GameManager.MIN_RATIO);
	}
	protected addToStage(e: Laya.Event): void {
		let s = this;
		s.start();
	}
	/**模块启动(addToStage的时候启动，重复添加到stage可多次启动)*/
	protected start(): void {
		let s = this;		
		s._aborted = false;
	}

	private _startTimeOut:number;
    

	/**场景就绪，过场拉开*/
	protected ready():void
	{

	}
	
	public show(pr: Laya.Sprite = null): void {
		let s = this;
		pr.addChild(s);
	}
	/**是否处于终止状态*/
	public get aborted():boolean
	{
		return this._aborted;
	}
	/**终止模块活动，当离开当前模块的环节时，主程序会调度此方法以通知模块进行活动停止*/
	public abort():void
	{
		let s = this;
		s._aborted = true;
		if(s._globalDispatcher.hasListener(Dispatcher.MODULE_ABORT))
		s._globalDispatcher.event(Dispatcher.MODULE_ABORT, s._module_data);
	}

	public hide(): void {
		let s = this;
		if (s.parent)
		{
			if((<any>s.parent).removeElement)
				(<any>s.parent).removeElement(s);			
			else
				(<any>s.parent).removeChild(s);			
			if(s._globalDispatcher.hasListener(Dispatcher.MODULE_HIDE))
				Dispatcher.getInstance().event(Dispatcher.MODULE_HIDE, s._module_data);
		}			
	}

	public dispose(disposeChild: boolean = true, removeChild: boolean = true, forceDispose: boolean = false): void {
		
		let s = this;		
		s.destroy(disposeChild);
	}
	/**处理模块适配*/
	public resize(w: number, h: number): void {
		let s = this;		
		s.resetSize(GameManager.DESIGN_WIDTH,GameManager.DESIGN_HEIGHT,GameManager.SCALE_MODE,GameManager.MAX_RATIO,GameManager.MIN_RATIO);
		//计算舞台相对模块的边框位置
		Laya.Point.TEMP.setTo(0, 0);
		if(SDK.container)
			SDK.container.localToGlobal(Laya.Point.TEMP, false);
		s.globalToLocal(Laya.Point.TEMP, false);
		s._frameBound.x = Math.floor(Laya.Point.TEMP.x);
		s._frameBound.y = Math.floor(Laya.Point.TEMP.y);		

		Laya.Point.TEMP.setTo(w, h);
		if(SDK.container)
			SDK.container.localToGlobal(Laya.Point.TEMP, false);
		s.globalToLocal(Laya.Point.TEMP, false);
		
		s._frameBound.width = Math.floor(Laya.Point.TEMP.x - s._frameBound.x);
		s._frameBound.height = Math.floor(Laya.Point.TEMP.y - s._frameBound.y);
	}
	public get frameBound():Laya.Rectangle
	{
		return this._frameBound;
	}
	public setData(d: any): void {
		let s = this;
		if(d == null)d = {};
		s._baseData = d;
		s._module_data = {};
	}
	public getData(): any {		
		return this._baseData;
	}
	/**重载场景*/
	public reloadScene(): void {

	}
	

}